<?php
/**
 * ShopEx licence
 *
 * @copyright  Copyright (c) 2005-2010 ShopEx Technologies Inc. (http://www.shopex.cn)
 * @license  http://ecos.shopex.cn/ ShopEx License
 */


class b2c_mdl_goods_opinions extends dbeav_model{

	/**
	 * 构造方法
	 * @param object model相应app的对象
	 * @return null
	 */
    public function __construct($app){
        parent::__construct($app);
//        $this->use_meta();
    }
//    var $subSdf = array(
//        'default' => array(
//            'goods_id' => array('name'),
//            'user_id' => array('name')
//        )   
//    );
	/**
	 * 保存的方法
	 * @param mixed 保存的数据内容
	 * @return boolean
	 */
    public function save(&$aData){
        //判断user_id是否是会员id
        //判断goods_id是否是商品id
        if(parent::save($aData)){
            return true;
        }else{
            return false;
        }
    }

    function getList($cols='*', $filter=array(), $offset=0, $limit=-1, $orderType=null){
        $filters = $filter;
        $orderType = !$orderType ? "id desc" : $orderType;
        $list = parent::getList('*',$filters,$offset,$limit,$orderType);
        foreach($list as $key=>$val){
            if($val['image']){
                $list[$key]['image'] =  "<img width=\"100\" src=\"".base_storager::image_path($val['image'],'s')."\" />" ;
                $list[$key]['image_url'] =  base_storager::image_path($val['image'],'l');
            }else{
                unset($list[$key]['image']);
            }
        }
        return $list;
    }
    function get_list($cols='*', $filter=array(), $offset=0, $limit=-1, $orderType=null){
        $filters = $filter;
        $list = $this->getList('*',$filters,$offset,$limit,$orderType);
        $userObject = kernel::single('b2c_user_object');
        $member_id = $userObject->get_member_id();
        
 //       var_dump($list);exit;
        foreach($list as $key=>$val){
            if($val['image']){
                $list[$key]['image'] =  $val['image_url'] ;
            }else{
                $list[$key]['image'] =  "" ;
            }
            $list[$key]['created'] =  date("Y-m-d H:i:s",$val['created']) ;
            $list[$key]['content'] =  $val['content'] ? $val['content'] : ""  ;
            $member_lv=  kernel::single('b2c_mdl_member_lv');
            $image = app::get("b2c")->model("goods_opinions_image")->getList("*",array('opinions_id'=>$val['id']));
            $goods = app::get("b2c")->model("goods")->getRow("*",array('goods_id'=>$val['goods_id']));
            $member = app::get("b2c")->model("members")->getRow("*",array('member_id'=>$val['member_id']));
            $member_lv_name = $member_lv->getList('name',array('member_lv_id'=>$member['member_lv_id']));
       
            $brand = app::get("b2c")->model("brand")->getRow("*",array('brand_id'=>$goods['brand_id']));
            if($member_id){
                $is_fans = app::get("b2c")->model("member_fans")->count(array('member_id'=>$val['member_id'],'fans_id'=>$member_id));
                $is_praise = app::get("b2c")->model("goods_opinions_praise")->count(array('opinions_id'=>$val['id'],'member_id'=>$member_id));
                $list[$key]['is_praise'] = $is_praise ? 1 : 0;
                $list[$key]['is_attention'] = $is_fans ? 1 : 0;
            }else{
                
                $list[$key]['is_praise'] = 0;
                $list[$key]['is_attention'] = 0;
            }
            
            if($image){
                foreach($image as $img){
                    $list[$key]["tag"][$img['id']]['x'] = $img['add_x'];
                    $list[$key]["tag"][$img['id']]['y'] = $img['add_y'];
                    $list[$key]["tag"][$img['id']]['image_type'] = $img['type'];
                    $list[$key]["tag"][$img['id']]['image_tag'] = $img['content'];
                }
            }else{
                $list[$key]["tag"] = array();
            }
            $list[$key]['goods_name'] = $goods['name'];
            $list[$key]['brand_name'] = $brand['brand_name'];
            $list[$key]['name'] = $member['name'] ? $member['name'] : "[未设置]";
            $list[$key]['avatar'] =base_storager::image_path($member['avatar'],'l') ? base_storager::image_path($member['avatar'],'l') : kernel::get_resource_host_url()."/headPlaceHolder_big@2x.png";
            $list[$key]['member_lv_id'] = $member_lv_name[0]['name'];
            $list[$key]['fx'] = kernel::get_resource_host_url()."/wap/opinions-info.html?opinions_id=".$val['id'];
        }
        return $list;
    }
    function get_comment_list($member_id, $limit=-1, $offset=0){
        $sql = "select o.id,o.image,c.content,c.created,m.name,m.avatar from ".kernel::database()->prefix."b2c_goods_opinions as o "
                . "left join ".kernel::database()->prefix."b2c_goods_opinions_comment as c ON c.opinions_id = o.id "
                . "left join ".kernel::database()->prefix."b2c_members as m ON m.member_id = c.member_id "
                . "where o.image is not null and o.member_id = ".$member_id." order by c.id desc ";
        $list = kernel::database()->selectLimit($sql,$limit,$offset);
        foreach($list as $key=>$info){
            if($info['created']){
                $list[$key]['image'] = base_storager::image_path($info['image'],'l');
                $list[$key]['avatar'] = base_storager::image_path($info['avatar'],'l');
            }else{
                unset($list[$key]);
            }
        }
        return $list;
    }
    function get_praise_list($member_id, $limit=-1, $offset=0){
        
        $sql = "select o.id,o.image,p.created,m.name,m.avatar from ".kernel::database()->prefix."b2c_goods_opinions as o "
                . "left join ".kernel::database()->prefix."b2c_goods_opinions_praise as p ON p.opinions_id = o.id "
                . "left join ".kernel::database()->prefix."b2c_members as m ON m.member_id = p.member_id "
                . "where o.image is not null and o.member_id = ".$member_id." order by p.id desc ";
        $list = kernel::database()->selectLimit($sql,$limit,$offset);
        foreach($list as $key=>$info){
            if($info['created']){
                $list[$key]['image'] = base_storager::image_path($info['image'],'l');
                $list[$key]['avatar'] = base_storager::image_path($info['avatar'],'l');
            }else{
                unset($list[$key]);
            }
        }
        return $list;
    }
    
    function get_row($cols='*', $filter=array(), $orderType=null){
        $data = $this->get_list($cols, $filter, 0, 1, $orderType);
        if($data){
            return $data['0'];
        }else{
            return $data;
        }
    }
//    function pre_recycle($rows){
//    	$opinions_comment = $this->app->model('goods_opinions_comment');
//    	if(is_array($rows)){
//	    	foreach($rows as $bk=>$bv){
//				$cbrand = $opinions_comment->count(array('opinions_id'=>$bv['opinions_id']));
//				if($cbrand >0){
//                                    $this->recycle_msg = app::get('desktop')->_('该推荐下有评论');
//                                    return false;
//				}
//	    	}
//        }else{
//            
//        }
//        return true;
//    }
//
//   
    



}
